package top.codedance.iotp.simulator.serialPort.srs.opw;

import top.codedance.iotp.common.util.BytesBuilder;
import top.codedance.iotp.common.util.HexCodec;
import top.codedance.iotp.common.util.IEEE754.DataTransform;
import top.codedance.iotp.common.util.IEEE754.IEEE754Util;
import top.codedance.iotp.simulator.serialPort.srs.Simulator;

import java.util.ArrayList;
import java.util.List;

//奥科液位仪模拟器
public class Aoke implements Simulator {
    @Override
    public List<BytesBuilder> exec(byte[] cmd) {
        List<BytesBuilder> results = new ArrayList<>();
        HexCodec hc = new HexCodec();
        String command = hc.toHex(cmd).replaceAll("\\s+", "").toUpperCase();
        BytesBuilder bb = new BytesBuilder();
        StringBuffer sb = new StringBuffer();
        /**
         *  i201 TT YYMMDDHHmm
         *      TTpssssNNFFFFFFFF
         *      TTpssssNNFFFFFFFF
         *      TTpssssNNFFFFFFFF
         *      TTpssssNNFFFFFFFF
         *      &&CCCC
         *
         */
//        sb.append("i201 01 202308251320019000101" + DataTransform.bytesToHex(IEEE754Util.float2hex(1100f)) + "&&CCCC");
        // i201
        sb.append("i201".getBytes());
        //TT
        sb.append("01".getBytes());
        // YY 23
        sb.append(new byte[]{(char) 20});
        sb.append(new byte[]{(char) 23});
        //MM
        sb.append("08".getBytes());
        //DD
        sb.append("25".getBytes());
        //HH
        sb.append("17".getBytes());
        // mm
        sb.append("00".getBytes());
        // ------------------------------------------------------------------
        // TT
        sb.append("01".getBytes());
        // p
        sb.append("5".getBytes());
        // ssss
        sb.append("0001".getBytes());
        //NN
        sb.append("01".getBytes());
        //FFFFFFFF
        sb.append(DataTransform.bytesToHex(IEEE754Util.float2hex(1000f)).getBytes());
        // ------------------------------------------------------------------
        // TT
        sb.append("01".getBytes());
        // p
        sb.append("5".getBytes());
        // ssss
        sb.append("0001".getBytes());
        //NN
        sb.append("02".getBytes());
        //FFFFFFFF
        sb.append(DataTransform.bytesToHex(IEEE754Util.float2hex(2000f)).getBytes());
        //-------------------------------------------------------------------
        // TT
        sb.append("02".getBytes());
        // p
        sb.append("5".getBytes());
        // ssss
        sb.append("0001".getBytes());
        //NN
        sb.append("01".getBytes());
        //FFFFFFFF
        sb.append(DataTransform.bytesToHex(IEEE754Util.float2hex(3000f)).getBytes());
        //-------------------------------------------------------------------
        // &&
        sb.append("&&".getBytes());
        // CCCC
        sb.append("CCCC".getBytes());
        HexCodec hexCodec = new HexCodec();
        String hex = hexCodec.toHex(sb.toString().getBytes());
        results.add(bb.append(hex.getBytes()));
        BytesBuilder bytesBuilder = new BytesBuilder();
        StringBuffer sb2 = new StringBuffer();
        // i201
        sb2.append("i201".getBytes());
        //TT
        sb2.append("01".getBytes());
        // YY 23
        sb2.append(new byte[]{(char) 20});
        sb2.append(new byte[]{(char) 23});
        //MM
        sb2.append("08".getBytes());
        //DD
        sb2.append("25".getBytes());
        //HH
        sb2.append("17".getBytes());
        // mm
        sb2.append("00".getBytes());
        // ------------------------------------------------------------------
        // TT
        sb2.append("01".getBytes());
        // p
        sb2.append("5".getBytes());
        // ssss
        sb2.append("0001".getBytes());
        //NN
        sb2.append("01".getBytes());
        //FFFFFFFF
        sb2.append(DataTransform.bytesToHex(IEEE754Util.float2hex(1000f)).getBytes());
        //-------------------------------------------------------------------
        // &&
        sb2.append("&&".getBytes());
        // CCCC
        sb2.append("CCCC".getBytes());
        String hex1 = hexCodec.toHex(sb2.toString().getBytes());
        results.add(bytesBuilder.append(hex1.getBytes()));
        return results;
    }


}
